<?php

class UserAction extends Action {

    function index() {
        $user = M("users");
        $key = $this->_param("key");
        $this->assign("key", $key);
        if ($key) {
            $map = "name like '%" . $key . "%' or phone like '%" . $key . "%' or qq like '%" . $key . "%' or type like '%" . $key . "%'";
        } else {
            $map = "";
        }

        import('ORG.Util.Page'); // 导入分页类
        $count = $user->where($map)->order("")->count(); // 查询满足要求的总记录数
        $Page = new Page($count, 10);
        $show = $Page->show(); // 分页显示输出
        $list = $user->where($map)->order("")->limit($Page->firstRow . ',' . $Page->listRows)->select();
        $this->assign('page', $show); // 赋值分页输出
        $this->assign("list", $list);
        $this->display();
    }

    //添加会员
    function add() {
        $user = M("users");
        if ($_POST) {
            $data['name'] = $this->_param("name");
            $data['pwd'] = md5($this->_param("pwd"));
            $data['phone'] = $this->_param("phone");
            $data['qq'] = $this->_param("qq");
            $data['type'] = $this->_param("type");
            $data['money'] = $this->_param("money");
            $li = $user->add($data);
            if ($li) {
                $this->success("添加成功", "__URL__/");
            } else {
                $this->error("添加失败");
            }
        } else {
            $this->display();
        }
    }

    //编辑会员
    function edit() {
        $id = $this->_param("id");
        $user = M("users");
        if (!$_POST) {
            $list = $user->where("id = $id")->select();
            $this->assign("list", $list[0]);
            $this->display();
        } else {
            $data['name'] = $this->_param("name");
            $data['phone'] = $this->_param("phone");
            $data['qq'] = $this->_param("qq");
            $data['type'] = $this->_param("type");
            $data['money'] = $this->_param("money");
            $li = $user->where("id = $id")->save($data);
            if ($li >= 0) {
                $this->success("编辑成功", "__URL__/");
            }
        }
    }

    //删除会员
    function del() {
        $id = $this->_param("id");
        $user = M("users");
        $user->where("id = $id")->delete();
        if ($user) {
            $this->redirect("/User");
        }
    }
    
    //重置密码
    function repwd(){
        $id = $this->_param("id");
        $user = M("users");
        $data['pwd'] = md5("123456");
        $user->where("id= $id")->save($data);
        if ($user) {
            $this->redirect("/User");
        }
    }

}

?>
